//
//  ThreadBaseClass.m
//
//  Created by lianchuang  on 14-11-28
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "ThreadBaseClass.h"
#import "ThreadData.h"


NSString *const kThreadBaseClassStatus = @"status";
NSString *const kThreadBaseClassData = @"data";
NSString *const kThreadBaseClassIscollect = @"iscollect";
NSString *const kThreadBaseClassMsg = @"msg";
NSString *const kThreadBaseClassIssign = @"issign";


@interface ThreadBaseClass ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ThreadBaseClass

@synthesize status = _status;
@synthesize data = _data;
@synthesize iscollect = _iscollect;
@synthesize msg = _msg;
@synthesize issign = _issign;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.status = [[self objectOrNilForKey:kThreadBaseClassStatus fromDictionary:dict] doubleValue];
    NSObject *receivedThreadData = [dict objectForKey:kThreadBaseClassData];
    NSMutableArray *parsedThreadData = [NSMutableArray array];
    if ([receivedThreadData isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedThreadData) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedThreadData addObject:[ThreadData modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedThreadData isKindOfClass:[NSDictionary class]]) {
       [parsedThreadData addObject:[ThreadData modelObjectWithDictionary:(NSDictionary *)receivedThreadData]];
    }

    self.data = [NSMutableArray arrayWithArray:parsedThreadData];
            self.iscollect = [[self objectOrNilForKey:kThreadBaseClassIscollect fromDictionary:dict] doubleValue];
            self.msg = [self objectOrNilForKey:kThreadBaseClassMsg fromDictionary:dict];
            self.issign = [[self objectOrNilForKey:kThreadBaseClassIssign fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.status] forKey:kThreadBaseClassStatus];
    NSMutableArray *tempArrayForData = [NSMutableArray array];
    for (NSObject *subArrayObject in self.data) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForData addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForData addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForData] forKey:kThreadBaseClassData];
    [mutableDict setValue:[NSNumber numberWithDouble:self.iscollect] forKey:kThreadBaseClassIscollect];
    [mutableDict setValue:self.msg forKey:kThreadBaseClassMsg];
    [mutableDict setValue:[NSNumber numberWithDouble:self.issign] forKey:kThreadBaseClassIssign];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.status = [aDecoder decodeDoubleForKey:kThreadBaseClassStatus];
    self.data = [aDecoder decodeObjectForKey:kThreadBaseClassData];
    self.iscollect = [aDecoder decodeDoubleForKey:kThreadBaseClassIscollect];
    self.msg = [aDecoder decodeObjectForKey:kThreadBaseClassMsg];
    self.issign = [aDecoder decodeDoubleForKey:kThreadBaseClassIssign];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeDouble:_status forKey:kThreadBaseClassStatus];
    [aCoder encodeObject:_data forKey:kThreadBaseClassData];
    [aCoder encodeDouble:_iscollect forKey:kThreadBaseClassIscollect];
    [aCoder encodeObject:_msg forKey:kThreadBaseClassMsg];
    [aCoder encodeDouble:_issign forKey:kThreadBaseClassIssign];
}

- (id)copyWithZone:(NSZone *)zone
{
    ThreadBaseClass *copy = [[ThreadBaseClass alloc] init];
    
    if (copy) {

        copy.status = self.status;
        copy.data = [self.data copyWithZone:zone];
        copy.iscollect = self.iscollect;
        copy.msg = [self.msg copyWithZone:zone];
        copy.issign = self.issign;
    }
    
    return copy;
}


@end
